package graphics.seal.ui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import graphics.seal.ExtendFontCache;
import graphics.seal.ImageConvert3D;

public class MainFrame extends JFrame {

	private static final long serialVersionUID = 8933850727322190990L;

	public JButton generateBtn = new JButton("生成印章");
	public JLabel wordLabel = new JLabel("输入文字");
	public JLabel zodiacLabel = new JLabel("选择生肖");
	public JLabel fontLabel = new JLabel("选择字体");
	public JTextField word = new JTextField();
	public JComboBox<String> chineseZodiac = new JComboBox<String>();
	public JComboBox<String> fontComboBox = new JComboBox<String>();

	public MainFrame() {
		super();
		initialize();
	}

	private void initialize() {
		initializeChoice();

		this.setSize(300, 200);
		this.setLocation(500, 200);
		this.setTitle("Frame");

		JPanel panel = new JPanel();
		BoxLayout layout = new BoxLayout(panel, BoxLayout.Y_AXIS);
		panel.setLayout(layout);
		panel.add(zodiacLabel);
		panel.add(chineseZodiac);

		panel.add(fontLabel);
		panel.add(fontComboBox);

		panel.add(wordLabel);
		panel.add(word);
		panel.add(generateBtn);

		this.add(panel);
		this.setVisible(true);

		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				System.exit(0);
			}
		});

		generateBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String words = word.getText().trim();
				if (words.length() == 0)
					return;
				String sealName = chineseZodiac.getSelectedItem().toString();
				String fontName = fontComboBox.getSelectedItem().toString();
				BufferedImage binaryImage = ImageConvert3D.generateBinaryImage(words, Color.BLACK.getRGB(),
						getExtendFontKey(fontName));
				ImageConvert3D.generateSeal(ImageConvert3D.generateSealBottom(binaryImage), getSealOriginal(sealName),
						getSavePath(words, sealName, fontName));

				JOptionPane.showMessageDialog(null, "印章生成成功", "标题", JOptionPane.PLAIN_MESSAGE);
			}
		});
	}

	private void initializeChoice() {
		chineseZodiac.addItem("鼠");
		chineseZodiac.addItem("牛");
		chineseZodiac.addItem("虎");
		chineseZodiac.addItem("兔");
		chineseZodiac.addItem("龙");
		chineseZodiac.addItem("蛇");
		chineseZodiac.addItem("马");
		chineseZodiac.addItem("羊");
		chineseZodiac.addItem("猴");
		chineseZodiac.addItem("鸡");
		chineseZodiac.addItem("狗");
		chineseZodiac.addItem("猪");

		fontComboBox.addItem("篆体");
		fontComboBox.addItem("隶书");
	}

	private String getExtendFontKey(String fontName) {
		return "隶书".equals(fontName) ? ExtendFontCache.CLERICAL_SCRIPT_KEY : ExtendFontCache.SEAL_CHARACTER_KEY;
	}

	private String getSealOriginal(String sealName) {
		return System.getProperty("user.dir") + "/resource/models/" + sealName + ".stl";
	}

	private String getSavePath(String words, String sealName, String fontName) {
		String path = System.getProperty("user.dir") + "/seal-stl/" + words + "-" + sealName + "-" + fontName + "/";
		File f = new File(path);
		if (!f.exists()) {
			f.mkdirs();
		}
		return path + "test.stl";
	}

	public static void main(String[] args) {
		new MainFrame();
	}

}
